{extend name="public/base" /} {block name="style"}
<link rel="stylesheet" href="__LIB__/zTree/zTreeStyle.css" type="text/css"> {/block}{block name="body"}
<div class="content-tabs">
	<ol class="breadcrumb">
		<li>
			<a href="{:url('index/index')}"><i class="fa fa-home"></i></a>
		</li>
		<li>
			<a href="{:url('index')}">角色列表</a>
		</li>
		<li><strong>角色授权</strong></li>
	</ol>
</div>
<div class="row">
	<div class="col-sm-12">
		<div class="ibox float-e-margins">
			<div class="ibox-title">
				<h5>角色授权</h5>
			</div>
			<div class="ibox-content">
				<div class="form-group">
					<ul id="tree" class="ztree"></ul>
				</div>
				<div class="form-group">
					<input type="hidden" id="group_id" name="id" value="{$id}">
					<button class="btn btn-primary" id="auth-btn"><i class="fa fa-save"></i> 保存</button>&nbsp;&nbsp;
					<a href="{:url('index')}" class="btn btn-danger"><i class="fa fa-arrow-left"></i> 返回 </a>
				</div>
			</div>
		</div>
	</div>
</div>
{/block} {block name="script"}
<script src="__JS__/jquery.form.js"></script>
<script type="text/javascript" src="__LIB__/zTree/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="__LIB__/zTree/jquery.ztree.excheck-3.5.js"></script>
<script type="text/javascript" src="__LIB__//zTree/jquery.ztree.exedit-3.5.js"></script>
<script>
	$(document).ready(function() {
		/**
		 * 加载树形授权菜单
		 */
		var _id = $("#group_id").val();
		var tree = $("#tree");
		var zTree;

		// zTree 配置项
		var setting = {
			check: {
				enable: true
			},
			view: {
				dblClickExpand: true,
				showLine: true,
				showIcon: true,
				selectedMulti: false,
				fontCss: {
					"font-family": "微软雅黑",
					"font-size": "18px",
					"color": "red"
				}
			},
			data: {
				simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "pid",
					rootpid: ""
				},
				key: {
					name: "title"
				}
			}
		};
		$.ajax({
			url: "/index.php/admin/agent_role/getJson",
			type: "post",
			dataType: "json",
			cache: false,
			data: {
				id: _id
			},
			success: function(data) {
				zTree = $.fn.zTree.init(tree, setting, data);
				$.fn.zTree.getZTreeObj("tree").expandAll(true);
			}
		});

		/**
		 * 授权提交
		 */
		$("#auth-btn").on("click", function() {
			var checked_ids, auth_rule_ids = [];
			checked_ids = zTree.getCheckedNodes(); // 获取当前选中的checkbox
			$.each(checked_ids, function(index, item) {
				auth_rule_ids.push(item.id);
			});
			$.ajax({
				url: "/index.php/admin/agent_role/auth",
				type: "post",
				cache: false,
				data: {
					id: _id,
					auth_rule_ids: auth_rule_ids
				},
				success: function(data) {
					if(data.code === 1) {
						setTimeout(function() {
							location.href = data.url;
						}, 1000);
					}
					layer.msg(data.msg);
				}
			});
		});
	});
</script>
{/block}